<template>
  <div class="cont" v-show="contShow">
    <!-- 搜索工作栏 -->
    <el-form
      ref="queryFormRef"
      :inline="true"
      :model="queryParams"
      class="cont_top"
      label-width="50px"
      @submit.prevent="handleQuery"
    >
      <el-form-item style="margin-right: 12px">
        <el-button
          link
          type="primary"
          @click="addbtn"
          class="btnhover"
          style="
            width: 90px;
            height: 30px;
            background-color: rgba(218, 111, 40, 1);
            color: rgba(255, 255, 255, 1);
          "
          >新增</el-button
        >
        <el-button
          link
          type="primary"
          @click="batchDelete"
          class="btnhover"
          style="
            width: 90px;
            height: 30px;
            background-color: rgba(218, 111, 40, 1);
            color: rgba(255, 255, 255, 1);
          "
          >批量删除</el-button
        >
      </el-form-item>
      <el-form-item label="标题" prop="topic" style="margin-right: 17px">
        <el-input
          v-model="queryParams.topic"
          placeholder=""
          @keyup.enter="handleQuery"
          style="width: 200px"
        />
      </el-form-item>
      <el-form-item style="margin-right: 0">
        <el-button @click="handleQuery" class="btn btnsearch">查询</el-button>
        <el-button @click="resetQuery" class="btn btnreset">重置</el-button>
      </el-form-item>
    </el-form>
    <div style="display: flex">
      <!-- 选项卡· -->
      <div
        style="
          width: 226px;
          margin-right: 18px;
          height: 500px;
          border: 1px solid rgba(228, 228, 228, 1);
          margin-bottom: 70px;
        "
      >
        <div class="tabtop">分类</div>
        <div
          class="tabcont"
          v-for="(item, index) in optionarea"
          :key="index"
          :class="{ selected: isActive === index }"
          @click="colorbtn(index)"
          >{{ item.gridName }}
        </div>
      </div>
      <!-- 列表 -->
      <el-table
        v-loading="loading"
        element-loading-text="Loading..."
        :data="list"
        style="margin-bottom: 70px"
        :header-cell-style="{ background: '#F0F0F0' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="50" />
        <el-table-column align="center" label="标题" width="300">
          <template #default="scope">
            <div
              v-html="
                scope.row.topic.length > 18 ? scope.row.topic.slice(0, 18) + '...' : scope.row.topic
              "
              :title="scope.row.topic"
            >
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="图片" prop="attachmentUrl" width="120">
          <template #default="scope">
            <el-image style="width: 161px; height: 52px" :src="scope.row.attachmentUrl" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="发布时间" prop="createTime" width="200">
          <template #default="scope">
            {{ formatDate(scope.row.createTime, 'YYYY-MM-DD HH:mm ') }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="发布人" prop="creator" width="200" />
        <el-table-column align="center" label="操作" fixed="right" min-width="140">
          <template #default="scope">
            <el-button
              link
              type="primary"
              class="btnhover"
              @click="detailbtn(scope.row)"
              style="color: #3c88f0"
              >详情</el-button
            >
            <el-button
              link
              type="primary"
              class="btnhover"
              @click="editbtn(scope.row)"
              style="color: #3c88f0"
              >编辑</el-button
            >
            <el-button
              link
              type="primary"
              class="btnhover"
              @click="delbtn(scope.row)"
              style="color: #3c88f0"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <el-pagination
      style="
        font-size: 14px;
        position: absolute;
        bottom: 30px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
      "
      v-model:current-page="queryParams.pageNo"
      :hide-on-single-page="value"
      v-model:page-size="queryParams.pageSize"
      :page-sizes="pageSizes"
      layout="total,sizes,prev, pager, next,jumper"
      :total="total"
      @size-change="selectchange"
      @current-change="handleCurrentChange"
    />
  </div>
  <AddDialog
    v-show="addShow"
    :addShow="addShow"
    @getaddShow="getaddShow"
    :editData="editData"
    @geteditData="geteditData"
  />
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import AddDialog from './components/addDialog.vue'
import * as getSwiperApi from '@/api/appManagement/swiperManagement/index'
import { formatDate } from '@/utils/formatTime'
const { push } = useRouter() // 路由
const selectList = ref([]) // 表格选中数据
const loading = ref(true) // 列表加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const value = ref(true)
const pageSizes = [5, 10, 15, 20]
const isActive = ref(0)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 5,
  topic: '',
  carouselImageType: '党建首页'
})
const optionarea = [
  {
    id: 1,
    gridName: '党建首页'
  },
  {
    id: 2,
    gridName: '小程序首页'
  }
]
const queryFormRef = ref() // 搜索的表单
const addShow = ref(false)
const contShow = ref(true)
const editData = ref({})
const getaddShow = (val) => {
  addShow.value = val
  contShow.value = !val
}
const geteditData = (val) => {
  handleCurrentChange(val)
}

/** 条数变化 */
const selectchange = (e) => {
  queryParams.pageSize = e
  queryParams.pageNo = 1
  getList()
}
/** 页码变化 */
const handleCurrentChange = (e) => {
  queryParams.pageNo = e
  getList()
}

/** 查询任务列表 */
const getList = async () => {
  getSwiperApi
    .getswiperPage(queryParams)
    .then((res) => {
      let data = reactive(res.list)
      total.value = res.total
      list.value = data
      if (list.value.length === 0) {
        value.value = true
      } else {
        value.value = false
      }
      loading.value = false
    })
    .catch((error) => {
      loading.value = false
    })
}

/** 批量删除 */
const handleSelectionChange = (e) => {
  selectList.value = e
}
const batchDelete = () => {
  if (selectList.value.length == 0) {
    ElMessage({
      message: '至少选中一条数据！',
      type: 'warning'
    })
    return
  }

  ElMessageBox.confirm('此操作将永久删除选中轮播图, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      const idList = reactive([])
      selectList.value.forEach((item) => {
        idList.push(item.id)
      })
      getSwiperApi.deleteSwiper(idList).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

/** 选项卡颜色 */
const colorbtn = (index) => {
  isActive.value = index
  if (isActive.value == 0) {
    queryParams.carouselImageType = '党建首页'
    getList()
  } else {
    queryParams.carouselImageType = '小程序首页'
    getList()
  }
}

/** 新增 */
const addbtn = () => {
  addShow.value = true
  contShow.value = false
  editData.value = {}
  editData.value.dialogtitle = '新增图文'
  editData.value.pageNo = 1
  editData.value.carouselImageType = queryParams.carouselImageType
}
/** 编辑 */
const editbtn = (row: any) => {
  addShow.value = true
  contShow.value = false
  editData.value = row
  editData.value.dialogtitle = '编辑图文'
  editData.value.pageNo = queryParams.pageNo
}
/** 详情 */
const detailbtn = (row: any) => {
  push({
    name: 'swiperDetail',
    query: {
      id: row.id
    }
  })
}
/** 删除 */
const delbtn = (row: any) => {
  ElMessageBox.confirm('此操作将永久删除该轮播图, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      getSwiperApi.deleteSwiper([row.id]).then((res) => {
        if (res === true) {
          ElMessage.success('删除成功！')
          getList()
        }
      })
    })
    .catch(() => {
      ElMessage.info('已取消删除')
    })
}

/** 查询按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<style scoped>
.cont {
  background-color: rgba(255, 255, 255, 1);
  color: rgba(16, 16, 16, 1);
  font-size: 14px;
  text-align: center;
  padding: 20px;
  min-height: 660px;
  position: relative;
}

.cont_top {
  text-align: left;
  margin: 4px 0;
}

::v-deep .el-table .cell {
  display: flex;
  justify-content: center;
}

::v-deep .el-input__wrapper.is-focus {
  box-shadow: 0 0 0 1px #cc352f;
}

::v-deep .el-select__wrapper.is-focused {
  box-shadow: 0 0 0 1px #cc352f;
}

.btn {
  width: 80px;
  height: 30px;
  line-height: 20px;
  border-radius: 5px;
  font-size: 14px;
  text-align: center;
}

.btnreset {
  background-color: rgba(255, 255, 255, 1);
  border: 1px solid rgba(187, 187, 187, 1);
  color: rgba(142, 142, 142, 1);
}

.btnsearch {
  background-color: #cc352f;
  color: rgba(255, 255, 255, 1);
  border: 1px solid #cc352f;
}

.btn:hover {
  opacity: 0.8;
}

.btnhover:hover {
  opacity: 0.8;
}

.el-pagination {
  justify-content: center;
}

.namecont {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

::v-deep .el-form-item__label {
  padding: 0 17px 0 0;
}

.tabtop {
  width: 100%;
  height: 40;
  border-bottom: 1px solid rgba(228, 228, 228, 1);
  text-indent: 12px;
  text-align: left;
  line-height: 40px;
  font-weight: 550;
}

.tabcont {
  /* width: calc(100% -3px); */
  height: 40px;
  text-indent: 12px;
  text-align: left;
  line-height: 40px;
  margin-bottom: 5px;
  color: #8e8e8e;
  cursor: pointer;
  /* border-right: 3px solid #fff; */
}

.state_style {
  width: 70px;
  height: 22px;
  line-height: 22px;
  text-align: center;
  border-radius: 25px;
  padding: 0 5px;
  font-size: 12px;
  border: none;
  cursor: pointer;
  user-select: none;
}

.state_style:hover {
  opacity: 0.8;
}

.selected {
  height: 40px;
  line-height: 40px;
  background-color: rgba(188, 79, 79, 0.13);
  color: #cc352f;
}
</style>
